package de.tum.getraenke;

import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import java.util.Locale;

class TTS implements OnInitListener {
	private static TextToSpeech TTS;
	private String text;

	public TTS(Activity paramActivity, String paramString) {
		this.text = paramString;
		TTS = new TextToSpeech(paramActivity, this);
	}

	private void sayText(final String paramString) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				TTS.speak(paramString, 1, null);				
			}
			
		}).start();
	}

	public void finalize() {
		TTS.stop();
		TTS.shutdown();
	}

	public void onInit(int status) {
		Log.d("TTS", "init");
		if (status == TextToSpeech.SUCCESS) {
			int result = TTS.setLanguage(Locale.ENGLISH); // DEUTSCH
			if (result == TextToSpeech.LANG_MISSING_DATA ||
					result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TTS", "Sprache nicht verfügbar.");
			} else {
				sayText(text);
			}
		} else {
			Log.e("TTS", "Initialisierung fehlgeschlagen");
		}
	}

	public void stop() {
                TTS.stop();
		TTS.shutdown();
	}
}